package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {
    private ListView lss;
    private String[] names = {"香格里拉酒店","Kyrid酒店","Taicang Baolong"};
    private String[] prices = {"联系电话：13055596878","联系电话：13933386756","联系电话：15789556472"};
    private Integer[] icon = {R.drawable.fk2,R.drawable.kf3,R.drawable.kf4};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        lss = findViewById(R.id.ls);
        lss.setAdapter(new MyListAdapter());
    }

    // 修复1：将ViewHolder类移到外部作用域
    static class MyHolder {
        TextView bk, zz;
        ImageView tu;
    }

    // 修复2：类名改为符合Java规范的驼峰命名法
    class MyListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int i) {
            return names[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            MyHolder holder;

            if (view == null) {
                view = getLayoutInflater().inflate(R.layout.activity_item, viewGroup, false);
                holder = new MyHolder();
                holder.bk = view.findViewById(R.id.bk);
                holder.zz = view.findViewById(R.id.zz);
                holder.tu = view.findViewById(R.id.tu);

                // 修复3：保存ViewHolder引用
                view.setTag(holder);
            } else {
                holder = (MyHolder) view.getTag();
            }

            holder.bk.setText(names[i]);
            holder.zz.setText(prices[i]);
            holder.tu.setImageResource(icon[i]);

            return view;
        }
    }
    public void gotoMainActivity1(View view) {
        // 创建一个 Intent 对象，用于启动 MainActivity2
        Intent intent = new Intent(this, MainActivity_home.class);
        // 启动新的 Activity
        startActivity(intent);
    }
    public void gotoMainActivity_profile(View view) {
        // 创建一个 Intent 对象，用于启动 activity_profile
        Intent intent = new Intent(this, activity_profile.class);
        // 启动新的 Activity
        startActivity(intent);
    }
}